package com.wolf.etl.service.impl;

import com.wolf.etl.model.EtlTableFilterModel;
import com.wolf.etl.service.IEtlTableFilterService;
import com.wolf.provider.order.enumerate.QUERY_TYPE;
import com.wolf.provider.order.info.PageBean;
import com.wolf.provider.order.info.PageInfo;
import com.wolf.provider.order.info.ParamBean;
import com.wolf.provider.order.service.impl.BaseServiceImpl;
import com.wolf.util.NumberUtil;
import com.wolf.vo.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tommy's father on 2017/8/24.
 */
@Service("table_filter_service")
public class EtlTableFilterServiceImpl extends BaseServiceImpl<Long>
        implements IEtlTableFilterService {

    private static Logger logger = LoggerFactory.getLogger(EtlTableFilterServiceImpl.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.service.impl#EtlTableFilterServiceImpl";


    @Override
    public List<EtlTableFilterModel> findEtlFilterModels(Long tabId) {

        StringBuilder sql = new StringBuilder("select t.* from etl_table_filter t where t.etl_table_id=").append(tabId);
        sql.append(" union ");
        sql.append(" select t.* from etl_table_filter t where t.master_table_id=").append(tabId);

        Map<String, Object> map = new HashMap<>();

        return findModelListByMap(sql.toString(), map, new EtlTableFilterModel());
    }

    @Override
    public boolean testMasterTableFilter(Long tabId) {
        StringBuilder sql = new StringBuilder("select t.* from etl_table_filter t where t.master_table_id=").append(tabId);
        sql.append(" union ");
        sql.append(" select t.* from etl_table_filter t where t.master_table_id=").append(tabId);

        Map<String, Object> map = new HashMap<>();
        List<EtlTableFilterModel> models = findModelListByMap(sql.toString(), map, new EtlTableFilterModel());
        if (models.size() > 0) {
            return true;
        }
        return false;
    }


    @Override
    public PageResult findByPage(EtlTableFilterModel model, PageBean pageBean) {

        StringBuilder sql = new StringBuilder();
        sql.append("select * from etl_table_filter t where 1=1 ");

        List<ParamBean> paramBeans = new ArrayList<ParamBean>();

        if (NumberUtil.isNotEmpty(model.getClient_id())) {
            ParamBean paramBean = new ParamBean();
            paramBean.setWhereField("t.client_id");
            paramBean.setField("client_id");
            paramBean.setValue(model.getClient_id());
            paramBean.setType(QUERY_TYPE.EQUAL.getType());
            paramBeans.add(paramBean);
        }

        PageInfo<EtlTableFilterModel> vos =  findByPageInfo(sql.toString(),paramBeans,model,pageBean);

        PageResult result = new PageResult();
        result.buildResult(vos);
        return result;
    }

}
